|
In computing, the modulo operation finds the remainder after division of one number by another (sometimes called ''modulus''). Given two positive numbers, (the dividend) and (the divisor), ''a'' modulo ''n'' (abbreviated as ''a'' mod ''n'') is the remainder of the Euclidean division of ''a'' by ''n''. For instance, the expression "5 mod 2" would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0 because the division of 9 by 3 has a quotient of 3 and leaves a remainder of 0; there is nothing to subtract from 9 after multiplying 3 times 3. (Note that doing the division with a calculator will not show the result referred to here by this operation; the quotient will be expressed as a decimal fraction.) Although typically performed with ''a'' and ''n'' both being integers, many computing systems allow other types of numeric operands. The range of numbers for an integer modulo of ''n'' is 0 to ''n'' − 1. (''n'' mod 1 is always 0; ''n'' mod 0 is undefined, possibly resulting in a "Division by zero" error in computer programming languages.) See modular arithmetic for an older and related convention applied in number theory. When either or is negative, the naive definition breaks down and programming languages differ in how these values are defined. ==Remainder calculation for the modulo operation== In mathematics the result of the modulo operation is the remainder of the Euclidean division. However, other conventions are possible. Computers and calculators have various ways of storing and representing numbers; thus their definition of the modulo operation depends on the programming language and/or the underlying hardware. In nearly all computing systems, the quotient and the remainder of divided by satisfy : However, this still leaves a sign ambiguity if the remainder is nonzero: there are two possible choices for the remainder, one negative and the other positive, and there are also two possible choices for the quotient. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language and the signs of ''a'' and/or ''n''. Standard Pascal and Algol68 give a positive remainder (or 0) even for negative divisors, and some programming languages, such as C90, leave it up to the implementation when either of ''n'' or ''a'' is negative. See the table for details. ''a'' modulo 0 is undefined in the majority of systems, although some do define it to be ''a''. and thus according to equation () the remainder would have ''same sign as the dividend''. The quotient is rounded towards zero: equal to the first integer in the direction of zero from the exact rational quotient. : | Donald Knuth described floored division where the quotient is defined by the floor function and thus according to equation () the remainder would have the ''same sign as the divisor''. Due to the floor function, the quotient is always rounded downwards, even if it is already negative. : | Raymond T. Boute describes the Euclidean definition in which the remainder is always nonnegative, , and is therefore consistent with the Euclidean division algorithm. This convention is denoted ''Always positive'' in the table. In this case, : : or equivalently : where is the sign function, and thus : . | Common Lisp also defines round-division and ceiling-division where the quotient is given by and respectively. | IEEE 754 defines a remainder function where the quotient is rounded according to the round to nearest convention. Therefore, the sign of the remainder is chosen so as to be ''closest to zero''. }} As described by Leijen, 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「modulo operation」の詳細全文を読む スポンサード リンク
|